package cn.wolfcode.trip.base.util;

import cn.wolfcode.trip.base.domain.ShopCarItem;
import cn.wolfcode.trip.base.domain.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.Map;

public abstract class UserContext {
    public static final String User_IN_SESSION = "User_IN_SESSION" ;
    public static final String SHOPCAR_IN_SESSION = "SHOPCAR_IN_SESSION" ;
    public static HttpSession getSession(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest().getSession();
    }

    public static void setCurrentUser(User user){
        getSession().setAttribute(User_IN_SESSION,user);
    }


    public static User getCurrentUser(){
        return (User)getSession().getAttribute(User_IN_SESSION);
    }
    public static void setCurrentUserShopCar(Map<Long, ShopCarItem> shopCar){
        getSession().setAttribute(SHOPCAR_IN_SESSION,shopCar);
    }
    public static Map<Long, ShopCarItem> getCurrentUserShopCar(){
        return (Map<Long, ShopCarItem>)getSession().getAttribute(SHOPCAR_IN_SESSION);
    }
}
